const db = require('../db/index')
const moment = require('moment')
const {limitCallback} = require('../utils/index')
//获取报销列表
exports.getReimbursement = (req, res) => {
    db.query(`select count(r.id) as total from reimbursement as r inner join reimbursement_types as rt  inner join audit_control as ac on r.type_id=rt.id and r.id = ac.p_id and r.is_delete=0 and ac.state=${req.query.type} where r.user_id = ${req.user.id}`, (err, total) => {
        if (err) return res.send({
            code: 500,
            message: '查询失败',
            data: {}
        })



        db.query(`select r.id,r.create_time,r.use_time,r.detail,r.type_id,r.price,r.images,r.is_delete,rt.type,ac.state,ac.create_time as ac_create_time,ac.images as ac_images  from reimbursement as r inner join reimbursement_types as rt inner join audit_control as ac on r.type_id=rt.id and r.id = ac.p_id and r.is_delete=0 and ac.state=${req.query.type}  order by r.id desc limit ?,?`, [limitCallback(req.query.page,req.query.limit,total[0].total), req.query.limit], (err, result) => {
            if (err) return res.send({
                code: 500,
                message: err,
                data: {}
            })

            result.map((item)=>{
                item.create_time = moment(item.create_time).format("YYYY-MM-DD HH:mm:ss")
                item.use_time = moment(item.use_time).format("YYYY-MM-DD HH:mm:ss")
                if(item.images!=""){
                    item.images=item.images.split(",")
                }else{
                    item.images=[]
                }
            })


            res.send({
                code: 200,
                message: '查询成功',
                data: result,
                total: total[0].total
            })


        })

    })


}


//新增报账
exports.createReimbursement = (req, res) => {
    let time = moment().format('YYYY-MM-DD HH:mm:ss')
    db.query('insert into reimbursement set?', Object.assign(req.body, { create_time: time },{user_id:req.user.id}), (err, result) => {

        if (err) return res.send({
            code: 500,
            message: err,
            data: {}
        })

        if (result.affectedRows !== 1) return res.send({
            code: 500,
            message: '新增失败',
            data: {}
        })
        const { insertId } = result
        //生成审核表信息
        db.query('insert into audit_control set?', { create_time: time, edit_time: time, p_id: insertId }, (err, result) => {

            if (err) res.send({
                code: 500,
                message: err,
                data: {}
            })

            if (result.affectedRows !== 1) res.send({
                code: 500,
                message: "操作失败",
                data: {}
            })

            db.query(`select r.id,r.create_time,r.use_time,r.detail,r.type_id,r.price,r.images,r.is_delete,rt.type,ac.state,ac.create_time as ac_create_time,ac.images as ac_images  from reimbursement as r inner join reimbursement_types as rt inner join audit_control as ac on r.type_id=rt.id and r.id = ac.p_id  where r.id = ${insertId}`, (err, result) => {

                if (err) return res.send({
                    code: 500,
                    message: '操作失败',
                    data: {}
                })

                if(result.length!==1){
                    res.send({
                        code:500,
                        message:'查询失败',
                        data:result
                    })
                }

                result.map((item)=>{
                    item.create_time = moment(item.create_time).format("YYYY-MM-DD HH:mm:ss")
                    item.use_time = moment(item.use_time).format("YYYY-MM-DD HH:mm:ss")
                    if(item.images!=""){
                        item.images=item.images.split(",")
                    }else{
                        item.images=[]
                    }
                })

                res.send({
                    code: 200,
                    message: "新增成功",
                    data: result[0]
                })
            })

        })





    })
}

//修改报账
exports.updateReimbursement = (req, res) => {
    db.query('update reimbursement set? where id=?', [req.body, req.body.id], (err, result) => {
        if (err) return res.send({
            code: 500,
            message: err,
            data: {}
        })

        if (result.affectedRows !== 1) res.send({
            code: 500,
            message: "修改失败",
            data: {}
        })

        db.query('select r.id,r.create_time,r.use_time,r.detail,r.type_id,r.price,r.images,r.is_delete,rt.type,ac.state,ac.create_time as ac_create_time,ac.images as ac_images  from reimbursement as r inner join reimbursement_types as rt inner join audit_control as ac on r.id = ?',req.body.id,(err,result)=>{
            if (err) return res.send({
                code: 500,
                message: err,
                data: {}
            })

            result.map((item)=>{
                item.create_time = moment(item.create_time).format("YYYY-MM-DD HH:mm:ss")
                item.use_time = moment(item.use_time).format("YYYY-MM-DD HH:mm:ss")
                if(item.images!=""){
                    item.images=item.images.split(",")
                }else{
                    item.images=[]
                }
            })

            res.send({
                code: 200,
                message: "修改成功",
                data: result[0]
            })

        })


    })
}

//删除
exports.deleteReimbursement = (req, res) => {
    db.query('update reimbursement set is_delete=1 where id=?', req.body.id, (err, result) => {
        if (err) return res.send({
            code: 500,
            message: err,
            data: {}
        })

        if (result.affectedRows !== 1) return res.send({
            code: 500,
            message: "删除失败",
            data: {}
        })

        db.query('update audit_control set is_delete=1 where p_id = ?',req.body.id,(err,result)=>{
            if (err) return res.send({
                code: 500,
                message: err,
                data: {}
            })

            if (result.affectedRows !== 1) return res.send({
                code: 500,
                message: "删除失败",
                data: {}
            })

            res.send({
                code: 200,
                message: "删除成功",
                data: {}
            })
        })



    })
}

//详情
exports.detail = (req,res) =>{
    db.query('select r.id,r.create_time,r.use_time,r.detail,r.type_id,r.price,r.images,r.is_delete,rt.type,ac.state,ac.create_time as ac_create_time,ac.images as ac_images  from reimbursement as r inner join reimbursement_types as rt inner join audit_control as ac on r.id = ?',req.query.id,(err,result)=>{

        if(err) return res.send({
            code:500,
            message:err,
            data:{}
        })
        result.map((item)=>{
            item.create_time = moment(item.create_time).format("YYYY-MM-DD HH:mm:ss")
            item.use_time = moment(item.use_time).format("YYYY-MM-DD HH:mm:ss")
            if(item.images!=""){
                item.images=item.images.split(",")
            }else{
                item.images=[]
            }
        })
        res.send({
            code:200,
            message:'查询成功',
            data:result[0]
        })

    })
}

